//
//  EditPermissionController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "EditPermissionController.h"

#import "FormattingUtilities.h"

@implementation EditPermissionController

@synthesize delegate;
@synthesize dataSourceDictionary;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = LocalizedString(@"Add permission");
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(done:)];
    
    self.dataSourceDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], @"read",
                                 [NSNumber numberWithBool:NO], @"write",
                                 [NSNumber numberWithBool:NO], @"modifyACL",
                                 nil];    
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)viewDidUnload {
	self.dataSourceDictionary = nil;
	[nameCtl release];
	nameCtl = nil;
	[readCtl release];
	readCtl = nil;
	[writeCtl release];
	writeCtl = nil;
	[modifyCtl release];
	modifyCtl = nil;
}

- (void)dealloc {
    [super dealloc];
	
	[dataSourceDictionary release];
	[nameCtl release];
	[readCtl release];
	[writeCtl release];
	[modifyCtl release];
}

- (void)store:(NSMutableDictionary *)aPermission {
	[self.delegate permissionAdded:aPermission];
}

- (void)save:(id)sender {
	[self store:self.dataSourceDictionary];
	[self done:sender];
}

- (void)done:(id)sender {
	[self.navigationController popViewControllerAnimated:YES];
}

//MARK: Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *CellIdentifier;
    
	if (indexPath.row == 0) {
		CellIdentifier = @"cellTypeNameEdit";
	} else {
		CellIdentifier = @"cellTypeNameControl";
	}
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
	
	switch (indexPath.row) {
		case 0:
			[cell addSubview:self.nameCtl];
			cell.accessoryView = nil;
			cell.textLabel.text = @"";
			break;
		case 1:
			cell.accessoryView = self.readCtl;
			cell.textLabel.text = LocalizedString(@"Read");			
			break;
		case 2:
			cell.textLabel.text = LocalizedString(@"Write");
			cell.accessoryView = self.writeCtl;
			break;
		case 3:
			cell.textLabel.text = LocalizedString(@"Modify ACL");
			cell.accessoryView = self.modifyCtl;
			break;
	}
    
    return cell;
}

//MARK: -
//MARK: Controls and functions

- (id)nameCtl {
	return nil;
}

- (UISwitch *)readCtl {
	if (readCtl == nil) {
		readCtl = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 120.0, 40.0)];
		[readCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
		readCtl.backgroundColor = [UIColor clearColor];
	}
	
	readCtl.on = [[self.dataSourceDictionary objectForKey:@"read"] boolValue];
	
	return readCtl;
}

- (UISwitch *)writeCtl {
	if (writeCtl == nil) {
		writeCtl = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 120.0, 40.0)];
		[writeCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
		writeCtl.backgroundColor = [UIColor clearColor];
	}
	
	writeCtl.on = [[self.dataSourceDictionary objectForKey:@"write"] boolValue];
	
	return writeCtl;
}

- (UISwitch *)modifyCtl {
	if (modifyCtl == nil) {
		modifyCtl = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 120.0, 40.0)];
		[modifyCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
		modifyCtl.backgroundColor = [UIColor clearColor];
	}
	
	modifyCtl.on = [[self.dataSourceDictionary objectForKey:@"modifyACL"] boolValue];
	
	return modifyCtl;
}

- (void)switchAction:(id)sender {
	if (sender == readCtl) {
		[self.dataSourceDictionary setObject:[NSNumber numberWithBool:[sender isOn]] forKey:@"read"];
	} else if (sender == writeCtl) {
		[self.dataSourceDictionary setObject:[NSNumber numberWithBool:[sender isOn]] forKey:@"write"];
	} else if (sender == modifyCtl) {
		[self.dataSourceDictionary setObject:[NSNumber numberWithBool:[sender isOn]] forKey:@"modifyACL"];
	}
}

@end

